<?php
/**
 * @author StarboyChina
 * @created 2011 08 26
 */
 class curl{
 	private
 		$_reg_url = "/https?\W{3}([\w-]+\.)+([\w-]+)(\/.[^?]*){0,}\\?{0,}(.[^?]*)?/i",
	 	$_url,
	 	$_is_transfer,
	 	$_timeout,
	 	$_referer,
	 	$_curl,
	 	$_useragent;
 	public function __construct($url = NULL) {
 		if (!function_exists('curl_init')) {
            trigger_error('cURL Class - PHP was not built with --with-curl, rebuild PHP to use cURL.') ;
        }
        //$this->reset();
        $this->resetForSearch();
        $this->_url			= $url;
 	}
 	private function reset(){
 		$this->_curl			= NULL;
        $this->_is_transfer		= true;
        $this->_referer			= NULL;
        $this->_timeout			= 1;
        $this->_useragent		= NULL;//"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB6"
 	}
 	private function resetForSearch(){
 		$this->_curl			= NULL;
        $this->_is_transfer		= true;
        $this->_referer			= HTTP_URL;
        $this->_timeout			= GETPAGE_TIMEOUT;
        $this->_useragent		= NULL;
 	}
 	public function create($url=NULL){
 		if(!is_null($url))$this->_url = $url;
 		if(preg_match($this->_reg_url, $this->_url)){
 			$this->_curl	= curl_init();
 			curl_setopt($this->_curl, CURLOPT_URL, $this->_url);
 		}
 	}

 	public function execute(){
        if($this->_url) $this->create();
 		if(is_resource($this->_curl)){
 			if($this->_is_transfer){
 				curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, $this->_is_transfer);
 			}
 			if(is_numeric($this->_timeout)){
 				curl_setopt($this->_curl, CURLOPT_TIMEOUT,$this->_timeout);
 			} 
 			if(!is_null($this->_useragent)){
 				curl_setopt($this->_curl, CURLOPT_USERAGENT, $this->_useragent);
 			}
 			if(!is_null($this->_referer)){
 				curl_setopt($this->_curl, CURLOPT_REFERER, $this->_referer);
 			}
 			$body = curl_exec($this->_curl);
 			curl_close($this->_curl);
 			return $body;
        }
 		return "false";
 	}
 	public function __set($name,$value){
 		$this->$name = $value ;
 	}
 	public function __get($name){
 		return $this->$name;
	}
 }
 /*
 $test = new curl();
  $test->_url = "http://php.net/manual/zh/function.is-resource.php?test=fdsfd";
  echo $test->execute();
 */